home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DT_IF.PY < prev    next >
Encoding:
Python Source  |  1999-03-09  |  7.4 KB  |  212 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''Conditional insertion
  65.  
  66.        Conditional insertion is performed using 'if' and 'else'
  67.        commands.
  68.  
  69.        To include text when an object is true using the EPFS
  70.        format, use::
  71.  
  72.           %(if name)[
  73.                text 
  74.           %(if name)]
  75.  
  76.        To include text when an object is true using the HTML
  77.        format, use::
  78.  
  79.           <!--#if name-->
  80.                text 
  81.           <!--#/if name-->
  82.  
  83.        where 'name' is the name bound to the object.
  84.  
  85.        To include text when an object is false using the EPFS
  86.        format, use::
  87.  
  88.           %(else name)[
  89.                text 
  90.           %(else name)]
  91.  
  92.        To include text when an object is false using the HTML
  93.        format, use::
  94.  
  95.           <!--#else name-->
  96.                text 
  97.           <!--#/else name-->
  98.  
  99.        Finally to include text when an object is true and to
  100.        include different text when the object is false using the
  101.        EPFS format, use::
  102.  
  103.           %(if name)[
  104.                true text 
  105.           %(if name)]
  106.           %(else name)[
  107.                false text 
  108.           %(else name)]
  109.  
  110.        and to include text when an object is true and to
  111.        include different text when the object is false using the
  112.        HTML format, use::
  113.  
  114.           <!--#if name-->
  115.                true text 
  116.           <!--#else name-->
  117.                false text 
  118.           <!--#/if name-->
  119.  
  120.        Notes:
  121.  
  122.        - if a variable is nor defined, it is considered to be false.
  123.  
  124.        - A variable if only evaluated once in an 'if' tag.  If the value
  125.          is used inside the tag, including in enclosed tags, the
  126.          variable is not reevaluated.
  127.  
  128. ''' 
  129. __rcs_id__='$Id: DT_If.py,v 1.16 1999/03/10 00:15:07 klm Exp $'
  130. __version__='$Revision: 1.16 $'[11:-2]
  131.  
  132. from DT_Util import ParseError, parse_params, name_param, str
  133.  
  134. class If:
  135.     blockContinuations='else','elif'
  136.     name='if'
  137.     elses=None
  138.     expr=''
  139.  
  140.     def __init__(self, blocks):
  141.  
  142.         tname, args, section = blocks[0]
  143.         args=parse_params(args, name='', expr='')
  144.         name,expr=name_param(args,'if',1)
  145.         self.__name__= name
  146.         if expr is None: cond=name
  147.         else: cond=expr.eval
  148.         sections=[cond, section.blocks]
  149.  
  150.         if blocks[-1][0]=='else':
  151.             tname, args, section = blocks[-1]
  152.             del blocks[-1]
  153.             args=parse_params(args, name='')
  154.             if args:
  155.                 ename,expr=name_param(args,'else',1)
  156.                 if ename != name:
  157.                     raise ParseError, ('name in else does not match if', 'in')
  158.             elses=section.blocks
  159.         else: elses=None
  160.  
  161.         for tname, args, section in blocks[1:]:
  162.             if tname=='else':
  163.                 raise ParseError, (
  164.                     'more than one else tag for a single if tag', 'in')
  165.             args=parse_params(args, name='', expr='')
  166.             name,expr=name_param(args,'elif',1)
  167.             if expr is None: cond=name
  168.             else: cond=expr.eval
  169.             sections.append(cond)
  170.             sections.append(section.blocks)
  171.  
  172.         if elses is not None: sections.append(elses)
  173.  
  174.         self.simple_form=tuple(sections)
  175.  
  176. class Unless:
  177.     name='unless'
  178.     blockContinuations=()
  179.  
  180.     def __init__(self, blocks):
  181.         tname, args, section = blocks[0]
  182.         args=parse_params(args, name='', expr='')
  183.         name,expr=name_param(args,'unless',1)
  184.         if expr is None: cond=name
  185.         else: cond=expr.eval
  186.         self.simple_form=(cond,None,section.blocks)
  187.  
  188. class Else(Unless):
  189.     # The else tag is included for backward compatibility and is deprecated.
  190.     name='else'
  191.